<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
// import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { getOrderSplitStatics } from '@/api/dashboard'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    }
  },
  data() {
    return {
      orderSplitData: {
        totalNum: 0,
        normalNum: 0,
        specialNum: 0,
        integralNum: 0,
        welfareNum: 0
      },
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.getOrderSplitStatics()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    getOrderSplitStatics() {
      getOrderSplitStatics().then(response => {
        this.orderSplitData = response.data
        this.initChart()
      })
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c}件 ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['爱心扶贫订单', '特价商城订单', '积分商城订单', '福利兑换订单']
        },
        series: [
          {
            name: '订单分布占比',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: [
              { value: this.orderSplitData.normalNum, name: '爱心扶贫订单' },
              { value: this.orderSplitData.specialNum, name: '特价商城订单' },
              { value: this.orderSplitData.integralNum, name: '积分商城订单' },
              { value: this.orderSplitData.welfareNum, name: '福利兑换订单' }
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2000
          }
        ]
      })
    }
  }
}
</script>
